package com.bringway.p2p.controller;

import java.io.OutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.bringway.framework.controller.JsonController;
import com.bringway.framework.excel.ExcelOperUtil;
import com.bringway.framework.excel.ExcelSheet;
import com.bringway.framework.util.DateUtils;
import com.bringway.framework.util.RequestUtils;
import com.bringway.p2p.service.P2pDqscService;

import net.sf.json.JSONObject;

/**
 * @类名 P2pDqscController
 * @功能 TODO 点券商城 管理
 * @作者 js
 * @日期 2016年10月17日 下午4:08:01
 * 
 */
@Controller
@RequestMapping("/dqsc")
public class P2pDqscController extends JsonController {
	@Autowired
	private P2pDqscService p2pDqscService;

	/**
	 * @作者 js
	 * @方法名 queryProListAll
	 * @功能 TODO 查询商品列表
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/queryProListAll")
	public ModelAndView queryProListAll(HttpServletRequest request, HttpServletResponse response) throws Exception {
		Integer start = RequestUtils.getIntParameter(request, "start");
		Integer limit = RequestUtils.getIntParameter(request, "limit");
		Integer page = RequestUtils.getIntParameter(request, "page");//新版本,传参
		String startDate = RequestUtils.getStringParameter(request, "startDate");
		String endDate = RequestUtils.getStringParameter(request, "endDate");
		String proName = RequestUtils.getStringParameter(request, "proName");
		String proCode = RequestUtils.getStringParameter(request, "proCode");
		if (null!=page) {//新版本,传参
			start=(page-1)*limit;
		}
		JSONObject params = new JSONObject();
		JSONObject result = new JSONObject();
		params.put("start", start);
		params.put("limit", limit);
		params.put("startDate", startDate);
		params.put("endDate", endDate);
		params.put("proName", proName);
		params.put("proCode", proCode);
		List<Map<String, Object>> list = p2pDqscService.queryProListAll(params);
		int count = p2pDqscService.getCount(params);
		result.put("data", list);
		result.put("total", count);
		if (null!=page) {//新版本,传参
			int totalPage=count%limit==0?count/limit:count/limit+1;
			result.put("totalPage", totalPage);
		}
		serialize(request, response, result);
		return null;
	}

	/**
	 * @作者 js
	 * @方法名 insertPro
	 * @功能 TODO 添加商品
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/insertPro")
	public ModelAndView insertPro(HttpServletRequest request, HttpServletResponse response) throws Exception {
		JSONObject result = new JSONObject();
		Map<String, Object> paramMap = new HashMap<>();
		String spmc = RequestUtils.getStringParameter(request, "spmc");
		String spbh = RequestUtils.getStringParameter(request, "spbh");
		String djje = RequestUtils.getStringParameter(request, "djje");
		String spgg = RequestUtils.getStringParameter(request, "spgg");
		String kcsl = RequestUtils.getStringParameter(request, "kcsl");
		String kysl = RequestUtils.getStringParameter(request, "kysl");
		String px = RequestUtils.getStringParameter(request, "px");
		String picsl = RequestUtils.getStringParameter(request, "picsl");
		String pich = RequestUtils.getStringParameter(request, "pich");
		String pic = RequestUtils.getStringParameter(request, "pic");
		paramMap.put("spmc", spmc);
		paramMap.put("spbh", spbh);
		paramMap.put("djje", djje);
		paramMap.put("spgg", spgg);
		paramMap.put("kcsl", kcsl);
		paramMap.put("kysl", kysl);
		paramMap.put("px", px);
		paramMap.put("picsl", picsl);
		paramMap.put("pich", pich);
		paramMap.put("pic", pic);
		boolean isSuc = p2pDqscService.insertPro(paramMap);
		if (isSuc) {
			result.put("isSuc", true);
		} else {
			result.put("isSuc", false);
			result.put("msg", "新增失败!");
		}
		result.put("success", true);
		serialize(request, response, result);
		return null;
	}

	/**
	 * @作者 js
	 * @方法名 updatePro
	 * @功能 TODO 更新商品信息
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/updatePro")
	public ModelAndView updatePro(HttpServletRequest request, HttpServletResponse response) throws Exception {
		JSONObject result = new JSONObject();
		Map<String, Object> paramMap = new HashMap<>();
		String spmc = RequestUtils.getStringParameter(request, "spmc");
		String spbh = RequestUtils.getStringParameter(request, "spbh");
		String djje = RequestUtils.getStringParameter(request, "djje");
		String spgg = RequestUtils.getStringParameter(request, "spgg");
		String kcsl = RequestUtils.getStringParameter(request, "kcsl");
		String kysl = RequestUtils.getStringParameter(request, "kysl");
		String px = RequestUtils.getStringParameter(request, "px");
		String picsl = RequestUtils.getStringParameter(request, "picsl");
		String id = RequestUtils.getStringParameter(request, "id1");
		String pich = RequestUtils.getStringParameter(request, "pich");
		String pic = RequestUtils.getStringParameter(request, "pic");
		paramMap.put("spmc", spmc);
		paramMap.put("spbh", spbh);
		paramMap.put("djje", djje);
		paramMap.put("spgg", spgg);
		paramMap.put("kcsl", kcsl);
		paramMap.put("kysl", kysl);
		paramMap.put("px", px);
		paramMap.put("picsl", picsl);
		paramMap.put("id", id);
		paramMap.put("pich", pich);
		paramMap.put("pic", pic);
		boolean isSuc = p2pDqscService.updatePro(paramMap);
		if (isSuc) {
			result.put("isSuc", true);
		} else {
			result.put("isSuc", false);
			result.put("msg", "更新失败!");
		}
		result.put("success", true);
		serialize(request, response, result);
		return null;
	}

	/**
	 * @作者 js
	 * @方法名 queryShdzListAll
	 * @功能 TODO 查询用户地址列表
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/queryShdzListAll")
	public ModelAndView queryShdzListAll(HttpServletRequest request, HttpServletResponse response) throws Exception {
		JSONObject result = new JSONObject();
		JSONObject params = new JSONObject();
		Integer start = RequestUtils.getIntParameter(request, "start");
		Integer limit = RequestUtils.getIntParameter(request, "limit");
		Integer page = RequestUtils.getIntParameter(request, "page");//新版本,传参
		String yhm = RequestUtils.getStringParameter(request, "yhm");
		String shrxm = RequestUtils.getStringParameter(request, "shrxm");
		String sjhm = RequestUtils.getStringParameter(request, "sjhm");
		String sjhmBy = RequestUtils.getStringParameter(request, "sjhmBy");
		String shdz = RequestUtils.getStringParameter(request, "shdz");
		if (null!=page) {//新版本,传参
			start=(page-1)*limit;
		}
		params.put("start", start);
		params.put("limit", limit);
		params.put("yhm", yhm);
		params.put("shrxm", shrxm);
		params.put("sjhm", sjhm);
		params.put("sjhmBy", sjhmBy);
		params.put("shdz", shdz);
		List<Map<String, Object>> list = p2pDqscService.queryShdzListAll(params);
		int count = p2pDqscService.getShdzListCount(params);
		result.put("data", list);
		result.put("total", count);
		if (null!=page) {//新版本,传参
			int totalPage=count%limit==0?count/limit:count/limit+1;
			result.put("totalPage", totalPage);
		}
		serialize(request, response, result);
		return null;
	}

	/**	
	 * @作者 js
	 * @方法名 queryGwcListAll
	 * @功能 TODO 查询购物车信息
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/queryGwcListAll")
	public ModelAndView queryGwcListAll(HttpServletRequest request, HttpServletResponse response) throws Exception {
		JSONObject result = new JSONObject();
		JSONObject params = new JSONObject();
		Integer start = RequestUtils.getIntParameter(request, "start");
		Integer limit = RequestUtils.getIntParameter(request, "limit");
		Integer page = RequestUtils.getIntParameter(request, "page");//新版本,传参
		String startDate = RequestUtils.getStringParameter(request, "startDate");
		String endDate = RequestUtils.getStringParameter(request, "endDate");
		String yhm = RequestUtils.getStringParameter(request, "yhm");
		String spmc = RequestUtils.getStringParameter(request, "spmc");
		String gwczt = RequestUtils.getStringParameter(request, "gwczt");
		if (null!=page) {//新版本,传参
			start=(page-1)*limit;
		}
		params.put("start", start);
		params.put("limit", limit);
		params.put("startDate", startDate);
		params.put("endDate", endDate);
		params.put("yhm", yhm);
		params.put("spmc", spmc);
		params.put("gwczt", gwczt);
		List<Map<String, Object>> list = p2pDqscService.queryGwcListAll(params);
		int count = p2pDqscService.getGwcCount(params);
		result.put("data", list);
		result.put("total", count);
		if (null!=page) {//新版本,传参
			int totalPage=count%limit==0?count/limit:count/limit+1;
			result.put("totalPage", totalPage);
		}
		serialize(request, response, result);
		return null;
	}

	/**
	 * @作者 js
	 * @方法名 queryOrderListAll
	 * @功能 TODO 查询订单信息
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/queryOrderListAll")
	public ModelAndView queryOrderListAll(HttpServletRequest request, HttpServletResponse response) throws Exception {
		JSONObject result = new JSONObject();
		JSONObject params = new JSONObject();
		Integer start = RequestUtils.getIntParameter(request, "start");
		Integer limit = RequestUtils.getIntParameter(request, "limit");
		Integer page = RequestUtils.getIntParameter(request, "page");//新版本,传参
		String startDate = RequestUtils.getStringParameter(request, "startDate");
		String endDate = RequestUtils.getStringParameter(request, "endDate");
		String fkksDate = RequestUtils.getStringParameter(request, "fkksDate");
		String fkjsDate = RequestUtils.getStringParameter(request, "fkjsDate");
		String yhm = RequestUtils.getStringParameter(request, "yhm");
		String ddh = RequestUtils.getStringParameter(request, "ddh");
		String ddzt = RequestUtils.getStringParameter(request, "ddzt");
		String psfs = RequestUtils.getStringParameter(request, "psfs");
		if (null!=page) {//新版本,传参
			start=(page-1)*limit;
		}
		params.put("start", start);
		params.put("limit", limit);
		params.put("startDate", startDate);
		params.put("endDate", endDate);
		params.put("fkksDate", fkksDate);
		params.put("fkjsDate", fkjsDate);
		params.put("yhm", yhm);
		params.put("ddh", ddh);
		params.put("ddzt", ddzt);
		params.put("psfs", psfs);
		List<Map<String, Object>> list = p2pDqscService.queryOrderListAll(params);
		int count = p2pDqscService.getOrderCount(params);
		String sumJe = p2pDqscService.getSumOrderJe(params);
		result.put("data", list);
		result.put("total", count);
		result.put("sumJe", sumJe);
		if (null!=page) {//新版本,传参
			int totalPage=count%limit==0?count/limit:count/limit+1;
			result.put("totalPage", totalPage);
			//合计行 数据 开始
			Map<String, Object> map=new HashMap<>();
			map.put("USER_NAME", "合计:");//key是字段名
			map.put("REAL_JE", sumJe);//key是字段名
			result.put("userdata", map);
			//合计行 数据 结束
		}
		serialize(request, response, result);
		return null;
	}

	/**
	 * @作者 js
	 * @方法名 queryOrderProList
	 * @功能 TODO 查询订单中商品信息
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("queryOrderProList")
	public ModelAndView queryOrderProList(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String orderbh = RequestUtils.getStringParameter(request, "orderbh");
		JSONObject result = new JSONObject();
		List<Map<String, Object>> list = p2pDqscService.queryOrderProList(orderbh);
		int count = p2pDqscService.getOrderProCount(orderbh);
		result.put("data", list);
		result.put("total", count);
		serialize(request, response, result);
		return null;
	}

	/**
	 * @作者 js
	 * @方法名 insertOrderKdxx
	 * @功能 TODO 录入订单物流信息
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("insertOrderKdxx")
	public ModelAndView insertOrderKdxx(HttpServletRequest request, HttpServletResponse response) throws Exception {
		JSONObject result = new JSONObject();
		JSONObject params = new JSONObject();
		String orderbh = RequestUtils.getStringParameter(request, "orderbh");
		String kdgs = RequestUtils.getStringParameter(request, "kdgs");
		String kdbh = RequestUtils.getStringParameter(request, "kdbh");
		params.put("orderbh", orderbh);
		params.put("kdgs", kdgs);
		params.put("kdbh", kdbh);
		boolean isSuc = p2pDqscService.updateOrderKdxx(params);
		if (isSuc) {
			result.put("isSuc", true);
		} else {
			result.put("isSuc", false);
			result.put("msg", "录入失败!");
		}
		result.put("success", true);
		serialize(request, response, result);
		return null;
	}

	/**
	 * @作者 js
	 * @方法名 insertOrderOver
	 * @功能 TODO 确认收货
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("insertOrderOver")
	public ModelAndView insertOrderOver(HttpServletRequest request, HttpServletResponse response) throws Exception {
		JSONObject result = new JSONObject();
		JSONObject params = new JSONObject();
		String orderbh = RequestUtils.getStringParameter(request, "orderbh");
		String qsr = RequestUtils.getStringParameter(request, "qsr");
		params.put("orderbh", orderbh);
		params.put("qsr", qsr);
		boolean isSuc = p2pDqscService.updateOrderOver(params);
		if (isSuc) {
			result.put("isSuc", true);
		} else {
			result.put("isSuc", false);
		}
		result.put("success", true);
		serialize(request, response, result);
		return null;
	}

	/**
	 * @作者 js  
	 * @方法名 exportOrderExcelMethod  
	 * @功能 TODO 下载Excel 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("exportOrderExcelMethod")
	public ModelAndView exportOrderExcelMethod(HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		JSONObject params = new JSONObject();
		String startDate = RequestUtils.getStringParameter(request, "startDate");
		String endDate = RequestUtils.getStringParameter(request, "endDate");
		String yhm = RequestUtils.getStringParameter(request, "yhm");
		String ddh = RequestUtils.getStringParameter(request, "ddh");
		String ddzt = RequestUtils.getStringParameter(request, "ddzt");
		String psfs = RequestUtils.getStringParameter(request, "psfs");
		String fkksDate = RequestUtils.getStringParameter(request, "fkksDate");
		String fkjsDate = RequestUtils.getStringParameter(request, "fkjsDate");
		params.put("fkksDate", fkksDate);
		params.put("fkjsDate", fkjsDate);
		params.put("startDate", startDate);
		params.put("endDate", endDate);
		params.put("yhm", yhm);
		params.put("ddh", ddh);
		params.put("ddzt", ddzt);
		params.put("psfs", psfs);
		List<Map<String, Object>> list=p2pDqscService.exportOrderExcelMethod(params);
		List<String[]> dataList = com.bringway.p2p.constant.ExcelHeadConstant.getOrderExportSearchResultData(list);
		// 导出数据到Excel文件
		List<ExcelSheet> sheetList = new ArrayList<ExcelSheet>();
		ExcelSheet sheet = new ExcelSheet();
		sheet.setBodyList(dataList);
		sheet.setSheetName(DateUtils.getCurrentDataStr() + "点券商城订单信息");
		sheetList.add(sheet);
		OutputStream os = response.getOutputStream();
		String fileName = DateUtils.getCurrentDataStr() + "点券商城订单信息.xls";
		super.setDownLoadResponseHeader(request, response, fileName);
		response.setContentType("application/vnd.ms-excel");

		// 导出EXCEL
		ExcelOperUtil.writeExcel(sheetList, os);
		return null;
	}
	
	/**
	 * @作者 js  
	 * @方法名 exportOrderExcelMethod 1 
	 * @功能 TODO 下载Excel 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("exportOrderExcelMethod1")
	public ModelAndView exportOrderExcelMethod1(HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		JSONObject params = new JSONObject();
		String startDate = RequestUtils.getStringParameter(request, "startDate");
		String endDate = RequestUtils.getStringParameter(request, "endDate");
		String yhm = RequestUtils.getStringParameter(request, "yhm");
		String ddh = RequestUtils.getStringParameter(request, "ddh");
		String ddzt = RequestUtils.getStringParameter(request, "ddzt");
		String psfs = RequestUtils.getStringParameter(request, "psfs");
		String fkksDate = RequestUtils.getStringParameter(request, "fkksDate");
		String fkjsDate = RequestUtils.getStringParameter(request, "fkjsDate");
		params.put("fkksDate", fkksDate);
		params.put("fkjsDate", fkjsDate);
		params.put("startDate", startDate);
		params.put("endDate", endDate);
		params.put("yhm", yhm);
		params.put("ddh", ddh);
		params.put("ddzt", ddzt);
		params.put("psfs", psfs);
		List<Map<String, Object>> list=p2pDqscService.exportOrderExcelMethod1(params);
		List<String[]> dataList = com.bringway.p2p.constant.ExcelHeadConstant.getOrderExportSearchResultData1(list);
		// 导出数据到Excel文件
		List<ExcelSheet> sheetList = new ArrayList<ExcelSheet>();
		ExcelSheet sheet = new ExcelSheet();
		sheet.setBodyList(dataList);
		sheet.setSheetName(DateUtils.getCurrentDataStr() + "点券商城订单信息");
		sheetList.add(sheet);
		OutputStream os = response.getOutputStream();
		String fileName = DateUtils.getCurrentDataStr() + "点券商城订单信息(详细).xls";
		super.setDownLoadResponseHeader(request, response, fileName);
		response.setContentType("application/vnd.ms-excel");

		// 导出EXCEL
		ExcelOperUtil.writeExcel(sheetList, os);
		return null;
	}
	/**
	 * @作者 js  
	 * @方法名 queryCheckDqList  
	 * @功能 TODO 核对用户账户点券收支是否正常
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("queryCheckDqList")
	public ModelAndView queryCheckDqList(HttpServletRequest request, HttpServletResponse response) throws Exception {
		JSONObject result = new JSONObject();
		JSONObject params = new JSONObject();
		String start = RequestUtils.getStringParameter(request, "start");
		String limit = RequestUtils.getStringParameter(request, "limit");
		String yhm = RequestUtils.getStringParameter(request, "yhm");
		String hdcz = RequestUtils.getStringParameter(request, "hdcz");
		params.put("start", start);
		params.put("limit", limit);
		params.put("yhm", yhm);
		params.put("hdcz", hdcz);
		List<Map<String, Object>> list=p2pDqscService.queryCheckDqList(params);
		int count=p2pDqscService.getCheckDqCount(params);
		result.put("data", list);
		result.put("total", count);
		serialize(request, response, result);
		return null;
	}
}
